#include "StartMenuScreen.h"
#include "GameScreens.h"
#include "s3e.h"
#include "Iw2D.h"
#include "TouchArea.h"
#include "Sprite.h"
#include "il_assert.h"
#include "IwGx.h"
#include "AudioSystem.h"
#include "ScoreSystem.h"

TouchArea Buttons[8];
Sprite ButtonSprite[10];
Sprite MenuTitle[5];

extern AudioSystem* SoundSystem;

#define MAIN_MENU_SCREEN_RESOURCE_GROUP_FILE "menu/resource.group"
#define MAIN_MENU_SCREEN_RESOURCE_GROUP_NAME "menu"

//480x320.
#define TITLE_0_POSX	(480/2)-75
#define TITLE_0_POSY	25
#define TITLE_0_POSY_S	0

#define TITLE_1_POSX	(480/2)-45
#define TITLE_1_POSY	50

#define BUTTON_0_POSX	(480/2)-45
#define BUTTON_0_POSY	100

#define BUTTON_1_POSX	(480/2)-45
#define BUTTON_1_POSY	175

#define BUTTON_2_POSX	(480/2)-150-45
#define BUTTON_2_POSY	250

#define BUTTON_3_POSX	(480/2)-45 //+150
#define BUTTON_3_POSX_B	(480/2)-45+150
#define BUTTON_3_POSY	250


#define BUTTON_6_POSX	(float)((480/2)-50)
#define BUTTON_6_POSY	100

#define BUTTON_7_POSX	(480/2)-140
#define BUTTON_7_POSY	100

#define BUTTON_8_POSX	(480/2)+100
#define BUTTON_8_POSY	100

#define BUTTON_9_POSX	(480/2)-100
#define BUTTON_9_POSY	200

float BUTTON_9_FRAME = 0;

#define BUTTON_FAST		10.0f
#define BUTTON_SLOW		6.0f

int menuscreen = 0;
float OffsetMenuBoatX = 0, MenuBoatSpeed = 0;
bool MenuBoatsActive = false;
bool MenuActiveBoat[5];
uint menutouch;

StartMenuScreen::StartMenuScreen( ScoreSystem* score_system )
	: m_score_system( score_system )
{ }

void StartMenuScreen::Load()
{
	IwGetResManager()->LoadGroup( MAIN_MENU_SCREEN_RESOURCE_GROUP_FILE );
    CIwResGroup *resGroup = IwGetResManager()->GetGroupNamed( MAIN_MENU_SCREEN_RESOURCE_GROUP_NAME );
    
    IL_ASSERT( resGroup != NULL, "failed to load the ressource group" );                                
	
	ButtonSprite[0].Load("button_start");
	ButtonSprite[1].Load("button_options");
	ButtonSprite[2].Load("button_hscores");
	ButtonSprite[3].Load("button_credits");

	ButtonSprite[4].Load("button_o_back");
	ButtonSprite[5].Load("button_o_ok");

	ButtonSprite[6].Load("menu_boat2");
	ButtonSprite[7].Load("menu_vol_off");
	ButtonSprite[8].Load("menu_vol_on");

	ButtonSprite[9].Load("menu_boat3",200);

	MenuTitle[0].Load("title");
	MenuTitle[1].Load("title_options");
	MenuTitle[3].Load("menu_credits");

	Buttons[0].CalcSizeForSprite(ButtonSprite[0]);
	Buttons[1].CalcSizeForSprite(ButtonSprite[1]);
	Buttons[2].CalcSizeForSprite(ButtonSprite[2]);
	Buttons[3].CalcSizeForSprite(ButtonSprite[3]);
	Buttons[4].CalcSizeForSprite(ButtonSprite[6]);
	Buttons[5].CalcSizeForSprite(ButtonSprite[7]);
	Buttons[6].CalcSizeForSprite(ButtonSprite[8]);

	Buttons[7].SetSize(CIwFVec2(205,45)); //not sure if works with animated sprite...

	OffsetMenuBoatX = 0;
	MenuBoatsActive = false;
	MenuActiveBoat[0] = false;
	MenuActiveBoat[1] = false;
	MenuActiveBoat[2] = false;
	MenuActiveBoat[3] = false;
	MenuActiveBoat[4] = false;

	m_water.Load("watur","waturb");

}

void StartMenuScreen::Unload()
{
	ButtonSprite[0].Unload();
	ButtonSprite[1].Unload();
	ButtonSprite[2].Unload();
	ButtonSprite[3].Unload();

	ButtonSprite[4].Unload();
	ButtonSprite[5].Unload();

	ButtonSprite[6].Unload();
	ButtonSprite[7].Unload();
	ButtonSprite[8].Unload();

	ButtonSprite[9].Unload();

	MenuTitle[0].Unload();
	MenuTitle[1].Unload();
	MenuTitle[3].Unload();

	m_water.Unload();

	IwGetResManager()->DestroyGroup(MAIN_MENU_SCREEN_RESOURCE_GROUP_NAME);
}
		
// returns the id of the screen it should be switched to
// -1 if the screen shouldn't be switched
int StartMenuScreen::Update( float /*dt*/ )
{
	if( s3eKeyboardGetState( s3eKeyEnter ) & S3E_KEY_STATE_PRESSED ) {
		return GameScreens::MAIN_GAMEPLAY_SCREEN;
	}

	m_water.Update(0);


	OffsetMenuBoatX+=MenuBoatSpeed;

	s3ePointerState touch0state = s3ePointerGetTouchState( 0 );
	switch(menuscreen)
	{
	case 0:
		Buttons[0].SetPos(CIwFVec2(BUTTON_0_POSX,BUTTON_0_POSY));
		Buttons[1].SetPos(CIwFVec2(BUTTON_1_POSX,BUTTON_1_POSY));
		Buttons[2].SetPos(CIwFVec2(BUTTON_2_POSX,BUTTON_2_POSY));
		Buttons[3].SetPos(CIwFVec2(BUTTON_3_POSX,BUTTON_3_POSY));		

		if( touch0state & S3E_POINTER_STATE_RELEASED )
		{
			MenuBoatsActive = false;
			MenuActiveBoat[0] = false;
			MenuActiveBoat[1] = false;
			MenuActiveBoat[2] = false;
			MenuActiveBoat[3] = false;
			MenuBoatSpeed = 0;
			OffsetMenuBoatX = 0;
			if(Buttons[0].IsWithin(menutouch))
			{
				OffsetMenuBoatX = 0;
				MenuBoatsActive = true;
				MenuActiveBoat[0] = true;
				MenuActiveBoat[1] = false;
				MenuActiveBoat[2] = false;
				MenuActiveBoat[3] = false;
				MenuBoatSpeed = BUTTON_FAST;
			}

			if(Buttons[1].IsWithin(menutouch))
			{
				OffsetMenuBoatX = 0;
				MenuBoatsActive = true;
				MenuActiveBoat[0] = false;
				MenuActiveBoat[1] = true;
				MenuActiveBoat[2] = false;
				MenuActiveBoat[3] = false;
				MenuBoatSpeed = BUTTON_FAST;
			}

/*			if(Buttons[2].IsWithin(menutouch))
			{
				OffsetMenuBoatX = 0;
				MenuBoatsActive = true;
				MenuActiveBoat[0] = false;
				MenuActiveBoat[1] = false;
				MenuActiveBoat[2] = true;
				MenuActiveBoat[3] = false;
				MenuBoatSpeed = BUTTON_SLOW;
			}*/

			if(Buttons[3].IsWithin(menutouch))
			{
				OffsetMenuBoatX = 0;
				MenuBoatsActive = true;
				MenuActiveBoat[0] = false;
				MenuActiveBoat[1] = false;
				MenuActiveBoat[2] = false;
				MenuActiveBoat[3] = true;
				MenuBoatSpeed = BUTTON_FAST; //BUTTON_SLOW;
			}
		}

		if(MenuBoatsActive==true)
		{
			if(BUTTON_0_POSX+(OffsetMenuBoatX * MenuActiveBoat[0])>480)
			{
				MenuBoatsActive = false;
				MenuActiveBoat[0] = false;
				MenuActiveBoat[1] = false;
				MenuActiveBoat[2] = false;
				MenuActiveBoat[3] = false;
				OffsetMenuBoatX = 0;
				return 1;
			}
			if(BUTTON_1_POSX+(OffsetMenuBoatX * MenuActiveBoat[1])>480)
			{
				MenuBoatsActive = false;
				MenuActiveBoat[0] = false;
				MenuActiveBoat[1] = false;
				MenuActiveBoat[2] = false;
				MenuActiveBoat[3] = false;
				OffsetMenuBoatX = 0;

				menuscreen = 1;
			}
			if(BUTTON_2_POSX+(-OffsetMenuBoatX * MenuActiveBoat[2])<-100)
			{
				MenuBoatsActive = false;
				MenuActiveBoat[0] = false;
				MenuActiveBoat[1] = false;
				MenuActiveBoat[2] = false;
				MenuActiveBoat[3] = false;
				OffsetMenuBoatX = 0;

				menuscreen = 2;
			}
			if(BUTTON_3_POSX+(OffsetMenuBoatX * MenuActiveBoat[3])>480)
			{
				MenuBoatsActive = false;
				MenuActiveBoat[0] = false;
				MenuActiveBoat[1] = false;
				MenuActiveBoat[2] = false;
				MenuActiveBoat[3] = false;
				OffsetMenuBoatX = 0;

				menuscreen = 3;
			}
		}
		break;
	
	case 1:
		Buttons[2].SetPos(CIwFVec2(BUTTON_2_POSX,BUTTON_2_POSY));
		Buttons[3].SetPos(CIwFVec2(BUTTON_3_POSX_B,BUTTON_3_POSY));

		Buttons[4].SetPos(CIwFVec2((BUTTON_6_POSX-50)+(SoundSystem->GetVolume()*100),BUTTON_6_POSY));
		Buttons[5].SetPos(CIwFVec2(BUTTON_7_POSX,BUTTON_7_POSY));
		Buttons[6].SetPos(CIwFVec2(BUTTON_8_POSX,BUTTON_8_POSY));
		Buttons[7].SetPos(CIwFVec2(BUTTON_9_POSX,BUTTON_9_POSY));


		if( touch0state & S3E_POINTER_STATE_DOWN )
		{
			if(Buttons[4].IsWithin(menutouch))
			{
				float tempa = (BUTTON_6_POSX)+((SoundSystem->GetVolume())*100);
				float tempb = (float)s3ePointerGetTouchX(menutouch);
				float tempc = (tempb-tempa);
				float tempd = ((tempc)/100);
				SoundSystem->SetVolume(
				SoundSystem->GetVolume()+tempd);
			}
		}

		if( touch0state & S3E_POINTER_STATE_RELEASED )
		{
			OffsetMenuBoatX = 0;
			MenuBoatSpeed = 0;

			MenuBoatsActive = false;
			if(Buttons[2].IsWithin(menutouch))
			{
				OffsetMenuBoatX = 0;
				MenuBoatsActive = true;
				MenuActiveBoat[0] = false;
				MenuActiveBoat[1] = false;
				MenuActiveBoat[2] = true;
				MenuActiveBoat[3] = false;
				MenuBoatSpeed = BUTTON_SLOW;
			}

/*
			if(Buttons[3].IsWithin(menutouch))
			{
				///IMPLEMENT SAVE FUNCTION HERE... Should Save the volume... which should also be loaded when the game starts :P
				// Clearing highscores does not have to be saved, since it just clears it :P
				//SoundSystem->Play("crashwood_c");
			}
*/

			if(Buttons[5].IsWithin(menutouch))
			{
				SoundSystem->SetVolume(0);
			}
			if(Buttons[6].IsWithin(menutouch))
			{
				SoundSystem->SetVolume(1);
			}

			if(Buttons[7].IsWithin(menutouch))
			{
				MenuActiveBoat[4] = true;
				//Here is where you need to clear the highscores ;)
				m_score_system->SetupDefaults();
			}
		}

		if(MenuActiveBoat[4]==true)
		{
			BUTTON_9_FRAME+=0.4f;
			if(BUTTON_9_FRAME>5)BUTTON_9_FRAME=0, MenuActiveBoat[4]=false;
		}

		if(MenuBoatsActive==true)
		{

			if(BUTTON_2_POSX+(-OffsetMenuBoatX * MenuActiveBoat[2])<-100)
			{
				MenuBoatsActive = false;
				MenuActiveBoat[0] = false;
				MenuActiveBoat[1] = false;
				MenuActiveBoat[2] = false;
				MenuActiveBoat[3] = false;
				MenuActiveBoat[4] = false;
				OffsetMenuBoatX = 0;

				menuscreen = 0;
			}
		}
		break;

	case 2:
		Buttons[2].SetPos(CIwFVec2(BUTTON_2_POSX,BUTTON_2_POSY));
		if( touch0state & S3E_POINTER_STATE_RELEASED )
		{
			OffsetMenuBoatX = 0;
			MenuBoatSpeed = 0;

			MenuBoatsActive = false;
			if(Buttons[2].IsWithin(menutouch))
			{
				OffsetMenuBoatX = 0;
				MenuBoatsActive = true;
				MenuActiveBoat[0] = false;
				MenuActiveBoat[1] = false;
				MenuActiveBoat[2] = true;
				MenuActiveBoat[3] = false;
				MenuBoatSpeed = BUTTON_SLOW;
			}
		}

		if(MenuBoatsActive==true)
		{

			if(BUTTON_2_POSX+(-OffsetMenuBoatX * MenuActiveBoat[2])<-100)
			{
				MenuBoatsActive = false;
				MenuActiveBoat[0] = false;
				MenuActiveBoat[1] = false;
				MenuActiveBoat[2] = false;
				MenuActiveBoat[3] = false;
				OffsetMenuBoatX = 0;

				menuscreen = 0;
			}
		}
		break;
	case 3:
		Buttons[2].SetPos(CIwFVec2(BUTTON_2_POSX,BUTTON_2_POSY));
		if( touch0state & S3E_POINTER_STATE_RELEASED )
		{
			OffsetMenuBoatX = 0;
			MenuBoatSpeed = 0;

			MenuBoatsActive = false;
			if(Buttons[2].IsWithin(menutouch))
			{
				OffsetMenuBoatX = 0;
				MenuBoatsActive = true;
				MenuActiveBoat[0] = false;
				MenuActiveBoat[1] = false;
				MenuActiveBoat[2] = true;
				MenuActiveBoat[3] = false;
				MenuBoatSpeed = BUTTON_SLOW;
			}
		}

		if(MenuBoatsActive==true)
		{

			if(BUTTON_2_POSX+(-OffsetMenuBoatX * MenuActiveBoat[2])<-100)
			{
				MenuBoatsActive = false;
				MenuActiveBoat[0] = false;
				MenuActiveBoat[1] = false;
				MenuActiveBoat[2] = false;
				MenuActiveBoat[3] = false;
				OffsetMenuBoatX = 0;

				menuscreen = 0;
			}
		}
		break;
	}

	return -1;
}

void StartMenuScreen::Render()
{
	m_water.Draw();

	s3eDebugPrint(120, 150, "StartMenuScreen!", 0);

	switch(menuscreen)
	{
	case 0:
		MenuTitle[0].Draw(CIwSVec2(TITLE_0_POSX,TITLE_0_POSY));

		ButtonSprite[0].Draw(CIwSVec2(BUTTON_0_POSX+(int)(OffsetMenuBoatX * MenuActiveBoat[0]),BUTTON_0_POSY));
		ButtonSprite[1].Draw(CIwSVec2(BUTTON_1_POSX+(int)(OffsetMenuBoatX * MenuActiveBoat[1]),BUTTON_1_POSY));
	//	ButtonSprite[2].Draw(CIwSVec2(BUTTON_2_POSX+(int)(-OffsetMenuBoatX * MenuActiveBoat[2]),BUTTON_2_POSY));
		ButtonSprite[3].Draw(CIwSVec2(BUTTON_3_POSX+(int)(OffsetMenuBoatX * MenuActiveBoat[3]),BUTTON_3_POSY));
		break;
	case 1:
		MenuTitle[0].Draw(CIwSVec2(TITLE_0_POSX,TITLE_0_POSY_S));
		MenuTitle[1].Draw(CIwSVec2(TITLE_1_POSX,TITLE_1_POSY));

		ButtonSprite[4].Draw(CIwSVec2(BUTTON_2_POSX+(int)(-OffsetMenuBoatX * MenuActiveBoat[2]),BUTTON_2_POSY));
		// ButtonSprite[5].Draw(CIwSVec2(BUTTON_3_POSX_B+(int)(OffsetMenuBoatX * MenuActiveBoat[3]),BUTTON_3_POSY));

		ButtonSprite[6].Draw(CIwSVec2((int)((BUTTON_6_POSX-50.0f)+(SoundSystem->GetVolume()*100.0f)),(int)BUTTON_6_POSY));

		ButtonSprite[7].Draw(CIwSVec2(BUTTON_7_POSX,BUTTON_7_POSY));
		ButtonSprite[8].Draw(CIwSVec2(BUTTON_8_POSX,BUTTON_8_POSY));

		ButtonSprite[9].DrawFrame(CIwSVec2(BUTTON_9_POSX,BUTTON_9_POSY),(int)BUTTON_9_FRAME);
		break;
	case 2:
		ButtonSprite[4].Draw(CIwSVec2(BUTTON_2_POSX+(int)(-OffsetMenuBoatX * MenuActiveBoat[2]),BUTTON_2_POSY));
		break;
	case 3:
		MenuTitle[3].Draw(CIwSVec2(0,0));
		ButtonSprite[4].Draw(CIwSVec2(BUTTON_2_POSX+(int)(-OffsetMenuBoatX * MenuActiveBoat[2]),BUTTON_2_POSY));
		break;
	}
}